I have the following code:
var obj2 = new Object();
console.log(Object.isExtensible(obj2)); //returns 'true'
console.log(obj2.constructor.isExtensible()); //returns 'false'
Could anyone explain why the first console print returns boolean 'true' and the second one 'false' and what difference it makes if isExtensible() is called either way.
The .constructor of an instance refers to the class / function that instantiates an instance. Since you're doing
var obj2 = new Object();
this means that obj2.constructor is the same as Object.
So
obj2.constructor.isExtensible()
is calling Object.isExtensible, but without an argument. isExtensible requires an argument. If you pass it the obj2, it'll do what you expect:
var obj2 = new Object();
console.log(Object.isExtensible(obj2)); //returns 'true'
console.log(obj2.constructor.isExtensible(obj2));
console.log(obj2.constructor === Object);
But that's still confusing. I'd recommend never using obj2.constructor - just use Object instead, it'll make more sense.
Usually, you'd probably only want to use the .constructor property when you don't already have a reference to the class - otherwise, it's easier to just reference the class.